package co.gov.medellin.ssti.sub.web.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.springframework.beans.factory.annotation.Autowired;

import co.gov.medellin.ssti.sub.common.excepcion.SUBException;
import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;
import co.gov.medellin.ssti.sub.model.service.BeneficiarioService;
import co.gov.medellin.ssti.sub.model.service.VflexService;
import co.gov.medellin.ssti.sub.model.vo.BeneficiarioVO;
import co.gov.medellin.ssti.sub.model.vo.VflexVO;
import co.gov.medellin.ssti.sub.paoyer.administration.exception.AdministrationUserException;
import co.gov.medellin.ssti.sub.paoyer.administration.util.AccessControl;
import co.gov.medellin.ssti.sub.paoyer.common.exception.CommonUserException;
import co.gov.medellin.ssti.sub.paoyer.common.exception.UserException;

public class CargarBeneficiarioAction extends Action {
	@Autowired
	BeneficiarioService beneficiarioServiceImpl;
	@Autowired
	VflexService vflexServiceImpl;

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		ActionMessages messages = new ActionMessages();

		try {
			AccessControl access = new AccessControl();
			access.validateAccess(mapping, request, response);
			String strCode = request.getParameter("code");

			BeneficiarioVO objBeneVO = new BeneficiarioVO();
			objBeneVO.setCode(new Long(strCode));
			try {
				ArrayList<BeneficiarioVO> beneSelectList = (ArrayList<BeneficiarioVO>) beneficiarioServiceImpl
						.findBeneficiarioByCriteria(objBeneVO,
								"ConsultarBeneficiario");
				if (beneSelectList != null && beneSelectList.size() > 0) {
					objBeneVO = (BeneficiarioVO) beneSelectList.get(0);
				}
			} catch (DAOException e) {
				SUBException.logDebugError("", e);
			}

			Collection<VflexVO> comunas = null;
			Collection<VflexVO> barrios = null;
			Collection<VflexVO> zonas = null;
			Collection<VflexVO> hechosVictimizantes = null;
			try {
				comunas = vflexServiceImpl.findVflex(VflexService.COMUNA);
				barrios = vflexServiceImpl.findVflex(VflexService.BARRIO);
				zonas = vflexServiceImpl.findVflex(VflexService.ZONA);
				hechosVictimizantes = vflexServiceImpl
						.findVflex(VflexService.HECHO_VICTIMIZANTE);
				request.setAttribute("comunas", comunas);
				request.setAttribute("barrios", barrios);
				request.setAttribute("zonas", zonas);
				request.setAttribute("hechosVictimizantes", hechosVictimizantes);

			} catch (DAOException e) {
				SUBException.logDebugError("", e);
			}

			if (objBeneVO == null) {
				request.setAttribute("msg",
						"Ocurrio un error relacionado con el beneficiario.");
				return mapping.getInputForward();
			}

			request.setAttribute("beneficiario", objBeneVO);

		} catch (UserException ue) {
			messages.add("msgWarn", new ActionMessage("loginForm.message."
					+ ue.getExceptionVO().getId()));
			saveMessages(request, messages);
			if (ue.getExceptionVO().getId() == AdministrationUserException.ENDED_SESSION) {
				response.sendRedirect(request.getContextPath() + "/index.jsp");
			} else if (ue.getExceptionVO().getId() == CommonUserException.NOT_ALLOWED_RESOURCE) {
				response.sendRedirect(request.getContextPath()
						+ "/admin/tile/invalidatedResource.jsp");
			} else {
				return mapping.getInputForward();
			}
		}

		return mapping.findForward("success");
	}
}
